<template>
  <el-dialog title="角色权限" :visible="showDialog" @close="btnCancel">
    <!-- box-group 要绑定一个数组，不然会报错 -->
    <el-checkbox-group v-model="checkedList">
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>
    <template #footer>
      <el-row type="flex" justify="end">
        <el-button size="samll" type="primary" @click="btnOK">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-row>
    </template>
  </el-dialog>
</template>
<script>
import { getRoleList } from '@/api/setting'
import { getUserDetailById } from '@/api/user'
import { assignRole } from '@/api/employees'
export default {
  components: {},
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    userId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      // 总角色列表
      roleList: [],
      // 被选中的列表
      checkedList: []
    }
  },
  computed: {},
  watch: {},
  async created() {
    const { rows } = await getRoleList()
    this.roleList = rows
    // console.log(this.roleList)
  },
  mounted() {},
  methods: {
    async btnOK() {
    // 发请求
      await assignRole({
        id: this.userId,
        roleIds: this.checkedList
      })
      // 提示用户
      this.$message.success('操作成功')
      // 关闭弹窗
      this.$emit('update:showDialog', false)
    },
    btnCancel() {
      this.$emit('update:showDialog', false)
    },
    async getUserDetailById(id) {
      // 被点击时触发回显函数，获取用户详情，将原有的角色id设置为默认选项
      // 自己不能调用，父组件那边的点击事件里调用
      const { roleIds } = await getUserDetailById(id)
      //   console.log(666, roleIds)
      this.checkedList = roleIds
    }
  }
}
</script>

<style scoped lang="less">

</style>
